package jbp.service.impl;

import com.fasterxml.jackson.core.type.TypeReference;
import jbp.common.util.JsonUtil;
import jbp.domain.project.AddProjectDto;
import jbp.domain.project.EditProjectDto;
import jbp.domain.project.ProjectDetailDto;
import jbp.domain.project.ProjectListDto;
import jbp.entity.ProjectDetailEntity;
import jbp.entity.ProjectEntity;
import jbp.mapper.ProjectMapper;
import jbp.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ProjectServiceImpl implements ProjectService {
    @Autowired
    private ProjectMapper projectMapper;

    @Override
    public ProjectDetailDto Query(int id) {
        ProjectDetailDto detailDto = projectMapper.SelById(id).toDetailDto();
        detailDto.setProjectDetailObj(projectMapper.SelByProjectId(id));
        return detailDto;
        //return projectMapper.SelById(id).toDetailDto();
    }

    @Override
    public int Count(Map<String, Object> params) {
        return projectMapper.getCount(params);
    }

    @Override
    public List<ProjectListDto> Query(Map<String, Object> params) {
        List<ProjectEntity> entityList = projectMapper.SelList(params);
        List<ProjectListDto> dtoList = new ArrayList<ProjectListDto>();
        for(int i=0; i<entityList.size(); i++){
            ProjectEntity entity = entityList.get(i);
            entity.setProjectDetailObj(
                    projectMapper.SelByProjectId(entity.getId())
            );
            dtoList.add(entity.toListDto());
        }
        return dtoList;
    }

    @Override
    public boolean Add(AddProjectDto addDto) {
        boolean flag = false;
        ProjectDetailEntity jsonObj = new ProjectDetailEntity();
        if(!addDto.getDetailJsonStr().isEmpty()){
            jsonObj = JsonUtil.toObject(addDto.getDetailJsonStr()
                    , new TypeReference<ProjectDetailEntity>() {});

        }
        try {
            ProjectEntity entity = addDto.toEntity();
            entity.setProjectDetailObj(jsonObj);
            projectMapper.Add(entity);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean Update(EditProjectDto editDto) {
        boolean flag = false;
        ProjectDetailEntity jsonObj = new ProjectDetailEntity();
        if(!editDto.getDetailJsonStr().isEmpty()){
            jsonObj = JsonUtil.toObject(editDto.getDetailJsonStr()
                    , new TypeReference<ProjectDetailEntity>() {});

        }
        try {
            ProjectEntity entity = editDto.toEntity();
            entity.setProjectDetailObj(jsonObj);
            projectMapper.Update(entity);
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    @Override
    public boolean Delete(String id) {
        boolean flag = false;
        try{
            projectMapper.Delete(id);
            flag = true;
        } catch(Exception e) {
            e.printStackTrace();
        }
        return flag;
    }
}
